package com.deplume.ssm.controller.admin.home;

import com.deplume.ssm.pojo.home.Register;
import com.deplume.ssm.pojo.common.Result;
import com.deplume.ssm.service.inface.admin.home.IRegisterService;
import com.deplume.ssm.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by libing on 2016/7/23.
 */
@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    IRegisterService registerService;


    @RequestMapping("jumpToJsp")
    public String jumpToRegisterJsp(HttpSession session, Model model)throws Exception{
        return "home/register";
    }


    @RequestMapping("requestJsp")
    @ResponseBody
    public Result requestRegister(HttpServletRequest request,
                                  @RequestParam("email") String email,
                                  @RequestParam("password") String password,
                                  @RequestParam("type") int type)throws Exception{
        System.out.println("===================================");
        System.out.println("email:"+email);
        System.out.println("password:"+password);
        System.out.println("type:"+type);
        System.out.println("===================================");

        int flag = 0;

        Result result = new Result();

        if(registerService.isExistCustomer(email) != 0)
        {
            //用户已存在
            result.setSuccess(false);
            result.setMsg("用户已注册");
        }
        else if(registerService.isExistCompany(email) != 0)
        {
            //公司用户存在
            result.setSuccess(false);
            result.setMsg("用户已注册");

        }
        else {
            //用户不存在
            Register register = new Register();
            register.setEmail(email);
            register.setPassword(password);
            register.setIp(MyUtil.getIpAddress(request));
            if(type == 0) {
               flag = registerService.insertCustomer(register);
            }
            else{
               flag = registerService.insertCompany(register);
            }
            if(flag > 0) {
                result.setSuccess(true);
                result.setMsg("注册成功");
            }else {
                result.setSuccess(true);
                result.setMsg("注册成功");
            }
            result.setOther(register.getEmail());
            result.setJumpUrl("jumpJsp.action?email=" + result.getOther());
        }
        return result;
    }

    @RequestMapping("jumpDirectJsp")
    public String jumpToLogin(Model model, HttpServletRequest request)throws Exception{
        return "home/login";
    }


    //注册之后跳到登录页面
    @RequestMapping("jumpJsp")
    public String jumpToLogin(Model model, HttpServletRequest request,String email)throws Exception{
        System.out.println("=======================2");
        model.addAttribute("email",email);
        return "home/login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public  Result loginResult(HttpSession session,
                               @RequestParam("email") String email,
                               @RequestParam("password") String password,
                               @RequestParam(value = "jumpUrl",required = false) String jumpUrl)throws Exception{
       // System.out.println(register.toString());
        System.out.println("=======================2");
        System.out.println("email:"+email);
        //System.out.println("password:"+register.getPassword());
        System.out.println("=======================2");

        Result result = new Result();

        if(registerService.isExistCustomer(email) != 0)
        {
            String pw = registerService.getUserPW(email);
            pw = pw.substring(9);
            if(pw.equals(password))
            {
                if(session.getAttribute("user") == null) {
                    result.setSuccess(true);
                    result.setMsg("登录成功");
                    session.setAttribute("user", email);
                    session.setAttribute("isCom",false);
                    if (jumpUrl == null) {
                        result.setJumpUrl("returnHome.action");
                    } else {
                        result.setJumpUrl(jumpUrl);
                    }
                }else {
                    session.invalidate();
                    result.setSuccess(true);
                    result.setMsg("登录成功");
                    session.setAttribute("user", email);
                    session.setAttribute("isCom",false);
                    if (jumpUrl == null) {
                        result.setJumpUrl("returnHome.action");
                    } else {
                        result.setJumpUrl(jumpUrl);
                    }
                }
            }else {
                result.setSuccess(false);
                result.setMsg("密码错误");
            }
        }else if (registerService.isExistCompany(email) != 0){
            String comPw = registerService.getCompanyPW(email);
            comPw = comPw.substring(9);
            if(comPw.equals(password))
            {
                if(session.getAttribute("user") == null) {
                    result.setSuccess(true);
                    result.setMsg("登录成功");
                    session.setAttribute("user", email);
                    session.setAttribute("isCom",true);
                    if (jumpUrl == null) {
                        result.setJumpUrl("returnHome.action");
                    } else {
                        result.setJumpUrl(jumpUrl);
                    }
                }else {
                    session.invalidate();
                    result.setSuccess(true);
                    result.setMsg("登录成功");
                    session.setAttribute("user", email);
                    session.setAttribute("isCom",true);
                    if (jumpUrl == null) {
                        result.setJumpUrl("returnHome.action");
                    } else {
                        result.setJumpUrl(jumpUrl);
                    }
                }
            }else {
                result.setSuccess(false);
                result.setMsg("密码错误");
            }
        } else {
            result.setMsg("用户不存在");
            result.setSuccess(false);
        }

        return result;
    }



    @RequestMapping("returnHome")
    public String returnHomeJsp(HttpSession session,Model model)throws Exception{
        model.addAttribute("user",session.getAttribute("user"));
        return "home/index";
    }
}
